package com.xindao.photonics.fragments.manager;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;

import com.xindaoapp.happypet.R;

public abstract class BaseFragmentActivity extends FragmentActivity implements ISwitchFragment {
	private FragmentManager mFragmentManager;
	private long mOldTime;
	private final long SPACETIME = 3000;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
//		Class[] clzzs = new Class[] { ShoppingCartFragment.class };
		mFragmentManager = new FragmentManager(this, null, R.id.container);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mFragmentManager.isHasOtherOption()) {
				return true;
			}
			if (!mFragmentManager.backFragment()) {
//				long currentTime = System.currentTimeMillis();
//				if (currentTime - mOldTime > SPACETIME) {
//					Toast.makeText(getBaseContext(), R.string.quite_confirm, Toast.LENGTH_SHORT).show();
//					mOldTime = currentTime;
//				} else {
				// 返回到最后一个，直接退出Activity
				onBackPressed();
//				}
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public final void startTabFragment(Class<? extends BaseFragment> clzz) {
		mFragmentManager.startTabFragment(clzz);
	}

	/**
	 * 跳转新的Fragment
	 */
	@Override
	public final void startFragment(BaseFragment fragment, Bundle bundle) {
		fragment.setArguments(bundle);
		mFragmentManager.startFragment(fragment);
	}

	@Override
	public void startFragment(BaseFragment fragment) {
		mFragmentManager.startFragment(fragment);
	}

	@Override
	public boolean backFragment() {
		return mFragmentManager.backFragment();
	}

	/*@Override
	public boolean backFragment() {
		return backFragment(null);
	}*/

	@Override
	public void startFragmentAndClearStack(BaseFragment fragment, Bundle bundle) {
		fragment.setArguments(bundle);
		mFragmentManager.startFragmentAndClearStack(fragment);
	}

}
